#Goggin, Stephen, 2018, "Replication Data for "What goes with red and blue? Mapping partisan and ideological associations in the minds of voters"", https://doi.org/10.7910/DVN/RDQZUM, Harvard Dataverse

###Generated on 12/22/2018

##This document should contain all necessary code to replicate the analyses contained in the published work -- please contact Stephen Goggin (goggin@berkeley.edu) with any questions whatsoever.

##Note: should set some directory as your working directory with the two following CSV files containing the necessary data for analysis

party_conjoint <- read.csv("party_conjoint.csv")

ideology_conjoint <- read.csv("ideology_conjoint.csv")

iv_label_list_varlist <- c(
"Gender - Male - g*",
"Gender - Female - g1",
"Family - Unmarried - f*",
"Family - Married - f1",
"Family - Married with one son - f2",
"Family - Married with one daughter - f3",
"Family - Married with one daughter and one son - f4",
"Family - Married with three sons - f5",
"Family - Married with three daughters - f6",
"Religion - None Listed - r*",
"Religion - Catholic - r1",
"Religion - Jewish - r2",
"Religion - Mainline Protestant - r3",
"Religion - Evangelical Protestant - r4",
"Military - None - m*",
"Military - National Guard - m1",
"Military - US Army - m2",
"Military - Major in US Army - m3",
"Occupation - Retail Manager - o*",
"Occupation - Small Business Owner - o1",
"Occupation - Attorney - o2",
"Occupation - Doctor - o3",
"Occupation - CEO - o4",
"Occupation - Farmer - o5",
"Occupation - Teacher - o6",
"Occupation - Factory Foreman - o7",
"Occupation - Construction Contractor - o8",
"Occupation - Political Staffer - o9",
"Issue - Strengthening the economy - ia_*",
"Issue - Strengthening national defense - ia_1",
"Issue - Preventing future terrorist attacks - ia_2",
"Issue - Promoting strong moral values - ia_3",
"Issue - Addressing the immigration problem - ia_4",
"Issue - Tax reform - ia_5",
"Issue - Fighting against illegal drugs - ia_6",
"Issue - Reducing crime - ia_7",
"Issue - Promoting trade with other nations - ia_8",
"Issue - Reducing the budget deficit - ia_9",
"Issue - Creating new jobs - ia_10",
"Issue - Promoting energy independence - ia_11",
"Issue - Improving education - ia_12",
"Issue - Strengthening Social Security - ia_13",
"Issue - Preserving Medicare - ia_14",
"Issue - Protecting the environment - ia_15",
"Issue - Improving health care - ia_16",
"Issue - Assistance to the poor and needy - ia_17"
)

iv_label_list_forplot <- c(
"Gender - Male",
"Gender - Female",
"Family - Unmarried",
"Family - Married",
"Family - Married with one son",
"Family - Married with one daughter",
"Family - Married with one daughter and one son",
"Family - Married with three sons",
"Family - Married with three daughters",
"Religion - None Listed",
"Religion - Catholic",
"Religion - Jewish",
"Religion - Mainline Protestant",
"Religion - Evangelical Protestant",
"Military - None",
"Military - National Guard",
"Military - US Army",
"Military - Major in US Army",
"Occupation - Retail Manager",
"Occupation - Small Business Owner",
"Occupation - Attorney",
"Occupation - Doctor",
"Occupation - CEO",
"Occupation - Farmer",
"Occupation - Teacher",
"Occupation - Factory Foreman",
"Occupation - Construction Contractor",
"Occupation - Political Staffer",
"Issue - Strengthening the economy",
"Issue - Strengthening national defense",
"Issue - Preventing future terrorist attacks",
"Issue - Promoting strong moral values",
"Issue - Addressing the immigration problem",
"Issue - Tax reform",
"Issue - Fighting against illegal drugs",
"Issue - Reducing crime",
"Issue - Promoting trade with other nations",
"Issue - Reducing the budget deficit",
"Issue - Creating new jobs",
"Issue - Promoting energy independence",
"Issue - Improving education",
"Issue - Strengthening Social Security",
"Issue - Preserving Medicare",
"Issue - Protecting the environment",
"Issue - Improving health care",
"Issue - Assistance to the poor and needy"
)

party_dem_only <- subset(party_conjoint,party_conjoint$pid3withlean=="Democratic Respondent")
party_rep_only <- subset(party_conjoint,party_conjoint$pid3withlean=="Republican Respondent")
party_ind_only <- subset(party_conjoint,party_conjoint$pid3withlean=="Independent Respondent")

ideology_dem_only <- subset(ideology_conjoint,ideology_conjoint$pid3withlean=="Democratic Respondent")
ideology_rep_only <- subset(ideology_conjoint,ideology_conjoint$pid3withlean=="Republican Respondent")
ideology_ind_only <- subset(ideology_conjoint,ideology_conjoint$pid3withlean=="Independent Respondent")

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){ 
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

#The 4 models of guessing from the Party Conjoint (overall, dem_only, ind_only, rep_only)

attach(party_conjoint)
model1 <- lm(guess_rep~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model1.cluster <- clse.f(party_conjoint,model1,V101)
detach(party_conjoint)

attach(party_dem_only)
model2 <- lm(guess_rep~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model2.cluster <- clse.f(party_dem_only,model2,V101)
detach(party_dem_only)

attach(party_ind_only)
model3 <- lm(guess_rep~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model3.cluster <- clse.f(party_ind_only,model3,V101)
detach(party_ind_only)

attach(party_rep_only)
model4 <- lm(guess_rep~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model4.cluster <- clse.f(party_rep_only,model4,V101)
detach(party_rep_only)


#The 4 models of evaluation from the Party Conjoint (overall, dem_only, ind_only, rep_only)

attach(party_conjoint)
model5 <- lm(cand_eval~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model5.cluster <- clse.f(party_conjoint,model5,V101)
detach(party_conjoint)

attach(party_dem_only)
model6 <- lm(cand_eval~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model6.cluster <- clse.f(party_dem_only,model6,V101)
detach(party_dem_only)

attach(party_ind_only)
model7 <- lm(cand_eval~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model7.cluster <- clse.f(party_ind_only,model7,V101)
detach(party_ind_only)

attach(party_rep_only)
model8 <- lm(cand_eval~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model8.cluster <- clse.f(party_rep_only,model8,V101)
detach(party_rep_only)


#The 4 models of guessing from the Ideology Conjoint (overall, dem_only, ind_only, rep_only)

attach(ideology_conjoint)
model9 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model9.cluster <- clse.f(ideology_conjoint,model9,V101)
detach(ideology_conjoint)

attach(ideology_dem_only)
model10 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model10.cluster <- clse.f(ideology_dem_only,model10,V101)
detach(ideology_dem_only)

attach(ideology_ind_only)
model11 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model11.cluster <- clse.f(ideology_ind_only,model11,V101)
detach(ideology_ind_only)

attach(ideology_rep_only)
model12 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model12.cluster <- clse.f(ideology_rep_only,model12,V101)
detach(ideology_rep_only)

###Because "coeftest" won't let you coerce an object to a data frame or any workable format, but it will let you write it as a csv. So do this, then read it back in.
write.csv(model1.cluster,"model1.csv")
write.csv(model2.cluster,"model2.csv")
write.csv(model3.cluster,"model3.csv")
write.csv(model4.cluster,"model4.csv")
write.csv(model5.cluster,"model5.csv")
write.csv(model6.cluster,"model6.csv")
write.csv(model7.cluster,"model7.csv")
write.csv(model8.cluster,"model8.csv")
write.csv(model9.cluster,"model9.csv")
write.csv(model10.cluster,"model10.csv")
write.csv(model11.cluster,"model11.csv")
write.csv(model12.cluster,"model12.csv")

model1.cluster.df <- read.csv("model1.csv")
model2.cluster.df <- read.csv("model2.csv")
model3.cluster.df <- read.csv("model3.csv")
model4.cluster.df <- read.csv("model4.csv")
model5.cluster.df <- read.csv("model5.csv")
model6.cluster.df <- read.csv("model6.csv")
model7.cluster.df <- read.csv("model7.csv")
model8.cluster.df <- read.csv("model8.csv")
model9.cluster.df <- read.csv("model9.csv")
model10.cluster.df <- read.csv("model10.csv")
model11.cluster.df <- read.csv("model11.csv")
model12.cluster.df <- read.csv("model12.csv")

####Now bringing it all together in one big data frame

all_results <- data.frame(
model1.cluster.df$X,
model1.cluster.df$Estimate,
model1.cluster.df$Std..Error,
model2.cluster.df$Estimate,
model2.cluster.df$Std..Error,
model3.cluster.df$Estimate,
model3.cluster.df$Std..Error,
model4.cluster.df$Estimate,
model4.cluster.df$Std..Error,
model5.cluster.df$Estimate,
model5.cluster.df$Std..Error,
model6.cluster.df$Estimate,
model6.cluster.df$Std..Error,
model7.cluster.df$Estimate,
model7.cluster.df$Std..Error,
model8.cluster.df$Estimate,
model8.cluster.df$Std..Error,
model9.cluster.df$Estimate,
model9.cluster.df$Std..Error,
model10.cluster.df$Estimate,
model10.cluster.df$Std..Error,
model11.cluster.df$Estimate,
model11.cluster.df$Std..Error,
model12.cluster.df$Estimate,
model12.cluster.df$Std..Error)

colnames(all_results) <- c(
"var_name",
"partyguess",
"partyguess_se",
"partyguess_dem",
"partyguess_dem_se",
"partyguess_ind",
"partyguess_ind_se",
"partyguess_rep",
"partyguess_rep_se",
"eval",
"eval_se",
"eval_dem",
"eval_dem_se",
"eval_ind",
"eval_ind_se",
"eval_rep",
"eval_rep_se",
"ideologyguess",
"ideologyguess_se",
"ideologyguess_dem",
"ideologyguess_dem_se",
"ideologyguess_ind",
"ideologyguess_ind_se",
"ideologyguess_rep",
"ideologyguess_rep_se")

null_vector <- rep(0,25)

all_results_temp <- rbind(
null_vector,
all_results[2,],
null_vector,
all_results[3:8,],
null_vector,
all_results[9:12,],
null_vector,
all_results[13:15,],
null_vector,
all_results[16:24,],
null_vector,
all_results[25:41,])

#Labelling with the iv lists
all_results_labeled <- cbind(iv_label_list_forplot,iv_label_list_varlist,all_results_temp)

write.csv(all_results_labeled,"allresults.csv")


###################
###Other non-core analyses
###################
#Subsetting based on guessing same party

party_conjoint_guesssameparty <- subset(party_conjoint, pid3withlean=="Democratic Respondent" & guess_rep==0 | pid3withlean=="Republican Respondent" & guess_rep==1)
party_conjoint_guessotherparty <- subset(party_conjoint, pid3withlean=="Democratic Respondent" & guess_rep==1 | pid3withlean=="Republican Respondent" & guess_rep==0)


######
#Incorporating Sureness as DV
attach(party_conjoint)
party_conjoint$guess_rep_with_sureness <- ifelse(
	guess_rep == 1 & guess_sure == 1, 1, ifelse(
	guess_rep == 1 & guess_sure == (2/3), (6/7), ifelse(
	guess_rep == 1 & guess_sure == (1/3), (5/7), ifelse(
	guess_rep == 1 & guess_sure == 0, (4/7), ifelse(
	guess_rep == 0 & guess_sure == 0, (3/7), ifelse(
	guess_rep == 0 & guess_sure == (1/3), (2/7), ifelse(
	guess_rep == 0 & guess_sure == (2/3), (1/7), ifelse(
	guess_rep == 0 & guess_sure == 1, 0, NA)	)))))))
detach(party_conjoint)

attach(ideology_conjoint)
ideology_conjoint$guess_conservative_with_sureness <- ifelse(
	guess_conservative == 1 & guess_sure == 1, 1, ifelse(
	guess_conservative == 1 & guess_sure == (2/3), (6/7), ifelse(
	guess_conservative == 1 & guess_sure == (1/3), (5/7), ifelse(
	guess_conservative == 1 & guess_sure == 0, (4/7), ifelse(
	guess_conservative == 0 & guess_sure == 0, (3/7), ifelse(
	guess_conservative == 0 & guess_sure == (1/3), (2/7), ifelse(
	guess_conservative == 0 & guess_sure == (2/3), (1/7), ifelse(
	guess_conservative == 0 & guess_sure == 1, 0, NA)	)))))))
detach(ideology_conjoint)


################
#1. Guessing Ideology by Ideology

attach(ideology_conjoint_lib_only)
model1 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model1.cluster <- clse.f(ideology_conjoint_lib_only,model1,V101)
detach(ideology_conjoint_lib_only)

attach(ideology_conjoint_mod_only)
model2 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model2.cluster <- clse.f(ideology_conjoint_mod_only,model2,V101)
detach(ideology_conjoint_mod_only)

attach(ideology_conjoint_con_only)
model3 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model3.cluster <- clse.f(ideology_conjoint_con_only,model3,V101)
detach(ideology_conjoint_con_only)

################
#2. Guessing Ideology by Knowledge

attach(ideology_conjoint_highknow)
model4 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model4.cluster <- clse.f(ideology_conjoint_highknow,model4,V101)
detach(ideology_conjoint_highknow)

attach(ideology_conjoint_lowknow)
model5 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model5.cluster <- clse.f(ideology_conjoint_lowknow,model5,V101)
detach(ideology_conjoint_lowknow)

################
#3. Guessing Party by Knowledge

attach(party_conjoint_highknow)
model6 <- lm(guess_rep~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model6.cluster <- clse.f(party_conjoint_highknow,model6,V101)
detach(party_conjoint_highknow)

attach(party_conjoint_lowknow)
model7 <- lm(guess_rep~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model7.cluster <- clse.f(party_conjoint_lowknow,model7,V101)
detach(party_conjoint_lowknow)

################
#4. Evaluation Conditional on Guessing

attach(party_conjoint_guesssameparty)
model8 <- lm(cand_eval~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model8.cluster <- clse.f(party_conjoint_guesssameparty,model8,V101)
detach(party_conjoint_guesssameparty)

attach(party_conjoint_guessotherparty)
model9 <- lm(cand_eval~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model9.cluster <- clse.f(party_conjoint_guessotherparty,model9,V101)
detach(party_conjoint_guessotherparty)

################
#5. Sureness of Guessing as DV

attach(party_conjoint)
model10 <- lm(guess_rep_with_sureness~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model10.cluster <- clse.f(party_conjoint,model10,V101)
detach(party_conjoint)

attach(ideology_conjoint)
model11 <- lm(guess_conservative_with_sureness~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model11.cluster <- clse.f(ideology_conjoint,model11,V101)
detach(ideology_conjoint)

################
################
################
################

###Because "coeftest" won't let you coerce an object to a data frame or any workable format, but it will let you write it as a csv. So do this, then read it back in.
write.csv(model1.cluster,"model1.csv")
write.csv(model2.cluster,"model2.csv")
write.csv(model3.cluster,"model3.csv")
write.csv(model4.cluster,"model4.csv")
write.csv(model5.cluster,"model5.csv")
write.csv(model6.cluster,"model6.csv")
write.csv(model7.cluster,"model7.csv")
write.csv(model8.cluster,"model8.csv")
write.csv(model9.cluster,"model9.csv")
write.csv(model10.cluster,"model10.csv")
write.csv(model11.cluster,"model11.csv")

model1.cluster.df <- read.csv("model1.csv")
model2.cluster.df <- read.csv("model2.csv")
model3.cluster.df <- read.csv("model3.csv")
model4.cluster.df <- read.csv("model4.csv")
model5.cluster.df <- read.csv("model5.csv")
model6.cluster.df <- read.csv("model6.csv")
model7.cluster.df <- read.csv("model7.csv")
model8.cluster.df <- read.csv("model8.csv")
model9.cluster.df <- read.csv("model9.csv")
model10.cluster.df <- read.csv("model10.csv")
model11.cluster.df <- read.csv("model11.csv")

all_results <- data.frame(
model1.cluster.df$X,
model1.cluster.df$Estimate,
model1.cluster.df$Std..Error,
model2.cluster.df$Estimate,
model2.cluster.df$Std..Error,
model3.cluster.df$Estimate,
model3.cluster.df$Std..Error,
model4.cluster.df$Estimate,
model4.cluster.df$Std..Error,
model5.cluster.df$Estimate,
model5.cluster.df$Std..Error,
model6.cluster.df$Estimate,
model6.cluster.df$Std..Error,
model7.cluster.df$Estimate,
model7.cluster.df$Std..Error,
model8.cluster.df$Estimate,
model8.cluster.df$Std..Error,
model9.cluster.df$Estimate,
model9.cluster.df$Std..Error,
model10.cluster.df$Estimate,
model10.cluster.df$Std..Error,
model11.cluster.df$Estimate,
model11.cluster.df$Std..Error)

colnames(all_results) <- c(
"var_name",
"ideo_lib",
"ideo_lib_se",
"ideo_mod",
"ideo_mod_se",
"ideo_con",
"ideo_con_se",
"ideo_highknow",
"ideo_highknow_se",
"ideo_lowknow",
"ideo_lowknow_se",
"party_highknow",
"party_highknow_se",
"party_lowknow",
"party_lowknow_se",
"eval_guessameparty",
"eval_guesssameparty_se",
"eval_guessotherparty",
"eval_guessotherparty_se",
"partyguess_withsureness",
"partyguess_withsureness_se",
"ideologyguess_withsureness",
"ideologyguess_withsureness_se")

null_vector <- rep(0,23)

all_results_temp <- rbind(
null_vector,
all_results[2,],
null_vector,
all_results[3:8,],
null_vector,
all_results[9:12,],
null_vector,
all_results[13:15,],
null_vector,
all_results[16:24,],
null_vector,
all_results[25:41,])

#Labelling with the iv lists from the other .R file
all_results_labeled <- cbind(iv_label_list_forplot,iv_label_list_varlist,all_results_temp)

write.csv(all_results_labeled,"allresults_additional.csv")

#####Trichotimizing Knowledge

party_lowknow <- subset(party_conjoint,party_conjoint$trichot_know==0)
party_medknow <- subset(party_conjoint,party_conjoint$trichot_know==1)
party_highknow <- subset(party_conjoint,party_conjoint$trichot_know==2)

ideology_lowknow <- subset(ideology_conjoint,ideology_conjoint$trichot_know==0)
ideology_medknow <- subset(ideology_conjoint,ideology_conjoint$trichot_know==1)
ideology_highknow <- subset(ideology_conjoint,ideology_conjoint$trichot_know==2)

####
library(sandwich)
library(lmtest)

################
#Guessing Ideology by Knowledge

attach(ideology_lowknow)
model1 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model1.cluster <- clse.f(ideology_lowknow,model1,V101)
detach(ideology_lowknow)

attach(ideology_medknow)
model2 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model2.cluster <- clse.f(ideology_medknow,model2,V101)
detach(ideology_medknow)

attach(ideology_highknow)
model3 <- lm(guess_conservative~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model3.cluster <- clse.f(ideology_highknow,model3,V101)
detach(ideology_highknow)


################
#Guessing Party by Knowledge

attach(party_lowknow)
model4 <- lm(guess_rep~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model4.cluster <- clse.f(party_lowknow,model4,V101)
detach(party_lowknow)

attach(party_medknow)
model5 <- lm(guess_rep~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model5.cluster <- clse.f(party_medknow,model5,V101)
detach(party_medknow)

attach(party_highknow)
model6 <- lm(guess_rep~
g1+
f1+f2+f3+f4+f5+f6+
r1+r2+r3+r4+
m1+m2+m3+
o1+o2+o3+o4+o5+o6+o7+o8+o9+
ia_1+ia_2+ia_3+ia_4+ia_5+ia_6+ia_7+ia_8+ia_9+ia_10+ia_11+ia_12+ia_13+ia_14+ia_15+ia_16+ia_17)
model6.cluster <- clse.f(party_highknow,model6,V101)
detach(party_highknow)


################
################

###Because "coeftest" won't let you coerce an object to a data frame or any workable format, but it will let you write it as a csv. So do this, then read it back in.
write.csv(model1.cluster,"model1.csv")
write.csv(model2.cluster,"model2.csv")
write.csv(model3.cluster,"model3.csv")
write.csv(model4.cluster,"model4.csv")
write.csv(model5.cluster,"model5.csv")
write.csv(model6.cluster,"model6.csv")

model1.cluster.df <- read.csv("model1.csv")
model2.cluster.df <- read.csv("model2.csv")
model3.cluster.df <- read.csv("model3.csv")
model4.cluster.df <- read.csv("model4.csv")
model5.cluster.df <- read.csv("model5.csv")
model6.cluster.df <- read.csv("model6.csv")

all_results <- data.frame(
model1.cluster.df$X,
model1.cluster.df$Estimate,
model1.cluster.df$Std..Error,
model2.cluster.df$Estimate,
model2.cluster.df$Std..Error,
model3.cluster.df$Estimate,
model3.cluster.df$Std..Error,
model4.cluster.df$Estimate,
model4.cluster.df$Std..Error,
model5.cluster.df$Estimate,
model5.cluster.df$Std..Error,
model6.cluster.df$Estimate,
model6.cluster.df$Std..Error)

colnames(all_results) <- c(
"var_name",
"ideo_low",
"ideo_low_se",
"ideo_mid",
"ideo_mid_se",
"ideo_high",
"ideo_high_se",
"party_low",
"party_low_se",
"party_mid",
"party_mid_se",
"party_high",
"party_high_se")

null_vector <- rep(0,23)

all_results_temp <- rbind(
null_vector,
all_results[2,],
null_vector,
all_results[3:8,],
null_vector,
all_results[9:12,],
null_vector,
all_results[13:15,],
null_vector,
all_results[16:24,],
null_vector,
all_results[25:41,])

#Labelling with the iv lists from the other .R file
all_results_labeled <- cbind(iv_label_list_forplot,iv_label_list_varlist,all_results_temp)

write.csv(all_results_labeled,"allresults_knowtrichot.csv")

#############################
######Now for plotting the core results

results <- read.csv("allresults.csv")

###Now to create the plots:
#Fig 1: Party Guessing as DV
#Fig 2: Party Guessing as DV, by Respondent PID3
#Fig 3: Evaluation as DV
#Fig 4: Evaluation as DV, by Respondent PID3
#Fig 5: Ideology Guessing as DV
#Fig 6: Ideology Guessing as DV, by Respondent PID3

results$iv_label_list_forplot_alt <- c(
"Gender - Male",
"Gender - Female",
"Family - Unmarried",
"Family - Married",
"Family - Married with one son",
"Family - Married with one daughter",
"Family - Married with one daughter and one son",
"Family - Married with three sons",
"Family - Married with three daughters",
"Religion - None Listed",
"Religion - Catholic",
"Religion - Jewish",
"Religion - Mainline Protestant",
"Religion - Evangelical Protestant",
"Military - None",
"Military - National Guard",
"Military - US Army",
"Military - Major in US Army",
"Occupation - Retail Manager",
"Occupation - Small Business Owner",
"Occupation - Attorney",
"Occupation - Doctor",
"Occupation - CEO",
"Occupation - Farmer",
"Occupation - Teacher",
"Occupation - Factory Foreman",
"Occupation - Construction Contractor",
"Occupation - Political Staffer",
"Issue - Strengthening the economy",
"Issue - Strengthening national defense",
"Issue - Preventing future terrorist attacks",
"Issue - Promoting strong moral values",
"Issue - Addressing the immigration problem",
"Issue - Tax reform",
"Issue - Fighting against illegal drugs",
"Issue - Reducing crime",
"Issue - Promoting trade with other nations",
"Issue - Reducing the budget deficit",
"Issue - Creating new jobs",
"Issue - Promoting energy independence",
"Issue - Improving education",
"Issue - Strengthening Social Security",
"Issue - Preserving Medicare",
"Issue - Protecting the environment",
"Issue - Improving health care",
"Issue - Assistance to the poor and needy"
)

library(ggplot2)

results$iv_order <- factor(results$iv_label_list_forplot_alt, as.character(results$iv_label_list_forplot_alt))
attach(results)

###############
###Figure 1

minci <- (partyguess - (1.96*partyguess_se))
maxci <- (partyguess + (1.96*partyguess_se))
pdf("partyguess.pdf", width=9, height=9, pointsize=10)
ggplot(results, 
    aes(x=iv_order, y=partyguess, group=1)) + 
    geom_line(y=0,size=0.5,color="gray70") +
    scale_x_discrete(limits=rev(iv_order)) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci)) +
    geom_point(shape=21, size=2, fill="black") +
    ylim(-0.3,0.3) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    coord_flip() + 
    geom_vline(x=18.5,y=partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y=partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y=partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y=partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y=partyguess,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Guess Candidate is Republican)")
dev.off()

###############
###Figure 3

minci <- (eval - (1.96*eval_se))
maxci <- (eval + (1.96*eval_se))
pdf("eval.pdf", width=9, height=9, pointsize=10)
ggplot(results, 
    aes(x=iv_order, y=eval, group=1)) + 
    geom_line(y=0,size=0.5,color="gray70") + 
    scale_x_discrete(limits=rev(iv_order)) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci)) +
    geom_point(shape=21, size=2, fill="black") +
    ylim(-0.1,0.1) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    coord_flip() + 
    geom_vline(x=18.5,y=eval,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y=eval,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y=eval,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y=eval,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y=eval,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change in Evaluation")
dev.off()

###############
###Figure 5

minci <- (ideologyguess - (1.96*ideologyguess_se))
maxci <- (ideologyguess + (1.96*ideologyguess_se))
pdf("ideologyguess.pdf", width=9, height=9, pointsize=10)
ggplot(results, 
    aes(x=iv_order, y=ideologyguess, group=1)) + 
    geom_line(y=0,size=0.5,color="gray70") +
    scale_x_discrete(limits=rev(iv_order)) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci)) +
    geom_point(shape=21, size=2, fill="black") +
    ylim(-0.3,0.3) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    coord_flip() + 
    geom_vline(x=18.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y= ideologyguess,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Guess Candidate is Conservative)")
dev.off()


###########################
###########################
###########################
###########################


stacked_by_party <- data.frame(
iv_order=rev(c(as.character(iv_order),as.character(iv_order),as.character(iv_order))),
party=rev(c(rep("Democratic Respondent",46),rep("Independent Respondent",46),rep("Republican Respondent",46))),
partyguess=rev(c(partyguess_dem,partyguess_ind,partyguess_rep)),
partyguess_se=rev(c(partyguess_dem_se,partyguess_ind_se,partyguess_rep_se)),
eval=rev(c(eval_dem,eval_ind,eval_rep)),
eval_se=rev(c(eval_dem_se,eval_ind_se,eval_rep_se)),
ideologyguess=rev(c(ideologyguess_dem, ideologyguess_ind, ideologyguess_rep)),
ideologyguess_se=rev(c(ideologyguess_dem_se, ideologyguess_ind_se, ideologyguess_rep_se))
)
stacked_by_party$iv_order <- factor(stacked_by_party$iv_order,as.character(stacked_by_party$iv_order))

detach(results)
stacked_by_party <- subset(stacked_by_party,party=="Democratic Respondent" | party=="Republican Respondent")



attach(stacked_by_party)

###############
###Figure 2

pd <- position_dodge(.5)
minci <- (stacked_by_party$partyguess - (1.96*stacked_by_party$partyguess_se))
maxci <- (stacked_by_party$partyguess + (1.96*stacked_by_party$partyguess_se))


pdf("partyguess_byparty.pdf", width=9, height=9, pointsize=10)
ggplot(stacked_by_party, 
    aes(x=iv_order, y=partyguess, colour=party)) + 
    scale_color_manual(values = c("blue4","firebrick1")) +
    #scale_x_discrete(limits=rep(rev(iv_order),3) + 
    geom_hline(y=0,size=0.5,color="gray70") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.35,0.35) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(0.78,0.05),legend.title=element_blank()) +
    coord_flip() + 
    geom_vline(x=18.5,y=partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y=partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y=partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y=partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y=partyguess,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Guess Candidate is Republican)")
dev.off()

###############
###Figure 4

pd <- position_dodge(.5)
minci <- (stacked_by_party$eval - (1.96*stacked_by_party$eval_se))
maxci <- (stacked_by_party$eval + (1.96*stacked_by_party$eval_se))


pdf("eval_byparty.pdf", width=9, height=9, pointsize=10)
ggplot(stacked_by_party, 
    aes(x=iv_order, y=eval, colour=party)) + 
    geom_hline(y=0,size=0.5,color="gray70") +
    scale_color_manual(values = c("blue4","firebrick1")) +
    #scale_x_discrete(limits=rep(rev(iv_order),3) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.15,0.15) + 
    theme_bw() + 
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(0.78,0.25),legend.title=element_blank()) +
    coord_flip() +
    geom_vline(x=18.5,y=eval,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y=eval,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y=eval,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y=eval,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y=eval,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change in Evaluation")
dev.off()

###############
###Figure 6

pd <- position_dodge(.5)
minci <- (stacked_by_party$ideologyguess - (1.96*stacked_by_party$ideologyguess_se))
maxci <- (stacked_by_party$ideologyguess + (1.96*stacked_by_party$ideologyguess_se))


pdf("ideologyguess_byparty.pdf", width=9, height=9, pointsize=10)
ggplot(stacked_by_party, 
    aes(x=iv_order, y=ideologyguess, colour=party)) + 
    geom_hline(y=0,size=0.5,color="gray70") +
    scale_color_manual(values = c("blue4","firebrick1")) +
    #scale_x_discrete(limits=rep(rev(iv_order),3) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.4,0.4) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(0.78,0.04),legend.title=element_blank()) +
    coord_flip() + 
    geom_vline(x=18.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y= ideologyguess,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Guess Candidate is Conservative)")
dev.off()

#########################
##Now for the additional plots:

results <- read.csv("allresults_additional.csv")

###Now to create the plots:
#Fig 1: Ideology Guessing by Ideology
#Fig 2: Ideology Guessing by Knowledge
#Fig 3: Party Guessing by Knowledge
#Fig 4: Evaluation Conditional on Guessing
#Fig 5: Party Guessing with Sureness as DV
#Fig 6: Ideology Guessing with Sureness as DV

results$iv_label_list_forplot_alt <- c(
"Gender - Male",
"Gender - Female",
"Family - Unmarried",
"Family - Married",
"Family - Married with one son",
"Family - Married with one daughter",
"Family - Married with one daughter and one son",
"Family - Married with three sons",
"Family - Married with three daughters",
"Religion - None Listed",
"Religion - Catholic",
"Religion - Jewish",
"Religion - Mainline Protestant",
"Religion - Evangelical Protestant",
"Military - None",
"Military - National Guard",
"Military - US Army",
"Military - Major in US Army",
"Occupation - Retail Manager",
"Occupation - Small Business Owner",
"Occupation - Attorney",
"Occupation - Doctor",
"Occupation - CEO",
"Occupation - Farmer",
"Occupation - Teacher",
"Occupation - Factory Foreman",
"Occupation - Construction Contractor",
"Occupation - Political Staffer",
"Issue - Strengthening the economy",
"Issue - Strengthening national defense",
"Issue - Preventing future terrorist attacks",
"Issue - Promoting strong moral values",
"Issue - Addressing the immigration problem",
"Issue - Tax reform",
"Issue - Fighting against illegal drugs",
"Issue - Reducing crime",
"Issue - Promoting trade with other nations",
"Issue - Reducing the budget deficit",
"Issue - Creating new jobs",
"Issue - Promoting energy independence",
"Issue - Improving education",
"Issue - Strengthening Social Security",
"Issue - Preserving Medicare",
"Issue - Protecting the environment",
"Issue - Improving health care",
"Issue - Assistance to the poor and needy"
)

library(ggplot2)

results$iv_order <- factor(results$iv_label_list_forplot_alt, as.character(results$iv_label_list_forplot_alt))
attach(results)

###########################
###########################
###########################
###########################

stacked_by_ideology <- data.frame(
iv_order=rev(c(as.character(iv_order),as.character(iv_order),as.character(iv_order))),
ideology=rev(c(rep("Liberal Respondent",46),rep("Moderate Respondent",46),rep("Conservative Respondent",46))),
ideologyguess=rev(c(ideo_lib,ideo_mod,ideo_con)),
ideologyguess_se=rev(c(ideo_lib_se,ideo_mod_se,ideo_con_se))
)
stacked_by_ideology$iv_order <- factor(stacked_by_ideology$iv_order,as.character(stacked_by_ideology$iv_order))

stacked_by_ideology <- subset(stacked_by_ideology,ideology=="Liberal Respondent" | ideology=="Conservative Respondent")


###########################

stacked_by_knowledge <- data.frame(
iv_order=rev(c(as.character(iv_order),as.character(iv_order))),
high_know=rev(c(rep("High Knowledge Respondent",46),rep("Low Knowledge Respondent",46))),
ideologyguess=rev(c(ideo_highknow,ideo_lowknow)),
ideologyguess_se=rev(c(ideo_highknow_se,ideo_lowknow_se)),
partyguess=rev(c(party_highknow,party_lowknow)),
partyguess_se=rev(c(party_highknow_se,party_lowknow_se))
)
stacked_by_knowledge$iv_order <- factor(stacked_by_knowledge$iv_order,as.character(stacked_by_knowledge$iv_order))


###########################

stacked_by_sameother <- data.frame(
iv_order=rev(c(as.character(iv_order),as.character(iv_order))),
guess_same=rev(c(rep("Guessed Same Party",46),rep("Guessed Other Party",46))),
eval=rev(c(eval_guessameparty,eval_guessotherparty)),
eval_se=rev(c(eval_guesssameparty_se,eval_guessotherparty_se))
)
stacked_by_sameother$iv_order <- factor(stacked_by_sameother$iv_order,as.character(stacked_by_sameother$iv_order))


detach(results)

###########################
###############
###Figure 1

attach(stacked_by_ideology)

pd <- position_dodge(.5)
minci <- (stacked_by_ideology$ideologyguess - (1.96*stacked_by_ideology$ideologyguess_se))
maxci <- (stacked_by_ideology$ideologyguess + (1.96*stacked_by_ideology$ideologyguess_se))


pdf("~/Desktop/ideologyguess_byideology.pdf", width=9, height=9, pointsize=10)
ggplot(stacked_by_ideology, 
    aes(x=iv_order, y=ideologyguess, colour=ideology)) + 
    geom_hline(y=0,size=0.5,color="gray70") +
    scale_color_manual(values = c("firebrick1","blue4")) +
    #scale_x_discrete(limits=rep(rev(iv_order),3) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.4,0.4) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(0.78,0.04),legend.title=element_blank()) +
    coord_flip() + 
    geom_vline(x=18.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y= ideologyguess,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Guess Candidate is Conservative)")
dev.off()

detach(stacked_by_ideology)

###########################
###############
###Figure 2

attach(stacked_by_knowledge)

pd <- position_dodge(.5)
minci <- (stacked_by_knowledge$ideologyguess - (1.96* stacked_by_knowledge$ideologyguess_se))
maxci <- (stacked_by_knowledge$ideologyguess + (1.96* stacked_by_knowledge$ideologyguess_se))


pdf("~/Desktop/ideologyguess_byknowledge.pdf", width=9, height=9, pointsize=10)
ggplot(stacked_by_knowledge, 
    aes(x=iv_order, y=ideologyguess, colour=high_know)) + 
    geom_hline(y=0,size=0.5,color="gray70") +
    scale_color_manual(values = c("black","gray60")) +
    #scale_x_discrete(limits=rep(rev(iv_order),3) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.4,0.4) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(0.78,0.04),legend.title=element_blank()) +
    coord_flip() + 
    geom_vline(x=18.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y= ideologyguess,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Guess Candidate is Conservative)")
dev.off()

detach(stacked_by_knowledge)

###########################
###############
###Figure 3

attach(stacked_by_knowledge)

pd <- position_dodge(.5)
minci <- (stacked_by_knowledge$partyguess - (1.96* stacked_by_knowledge$partyguess_se))
maxci <- (stacked_by_knowledge$partyguess + (1.96* stacked_by_knowledge$partyguess_se))


pdf("~/Desktop/partyguess_byknowledge.pdf", width=9, height=9, pointsize=10)
ggplot(stacked_by_knowledge, 
    aes(x=iv_order, y=partyguess, colour=high_know)) + 
    geom_hline(y=0,size=0.5,color="gray70") +
    scale_color_manual(values = c("black","gray60")) +
    #scale_x_discrete(limits=rep(rev(iv_order),3) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.4,0.4) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(0.78,0.04),legend.title=element_blank()) +
    coord_flip() + 
    geom_vline(x=18.5,y= partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y= partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y= partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y= partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y= partyguess,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Guess Candidate is Republican)")
dev.off()

detach(stacked_by_knowledge)

###########################
###############
###Figure 4

attach(stacked_by_sameother)

pd <- position_dodge(.5)
minci <- (stacked_by_sameother$eval - (1.96* stacked_by_sameother$eval_se))
maxci <- (stacked_by_sameother$eval + (1.96* stacked_by_sameother$eval_se))


pdf("~/Desktop/eval_bysamepartyguess.pdf", width=9, height=9, pointsize=10)
ggplot(stacked_by_sameother, 
    aes(x=iv_order, y=eval, colour=guess_same)) + 
    geom_hline(y=0,size=0.5,color="gray70") +
    scale_color_manual(values = c("gray60","black")) +
    #scale_x_discrete(limits=rep(rev(iv_order),3) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.1,0.1) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(0.78,0.04),legend.title=element_blank()) +
    coord_flip() + 
    geom_vline(x=18.5,y= eval,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y= eval,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y= eval,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y= eval,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y= eval,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change in Evaluation")
dev.off()

detach(stacked_by_sameother)

###########################
###############
###Figure 5

attach(results)

minci <- (partyguess_withsureness - (1.96*partyguess_withsureness_se))
maxci <- (partyguess_withsureness + (1.96*partyguess_withsureness_se))
pdf("~/Desktop/partyguess_withsurenessDV.pdf", width=9, height=9, pointsize=10)
ggplot(results, 
    aes(x=iv_order, y=partyguess_withsureness, group=1)) + 
    geom_line(y=0,size=0.5,color="gray70") +
    scale_x_discrete(limits=rev(iv_order)) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci)) +
    geom_point(shape=21, size=2, fill="black") +
    ylim(-0.3,0.3) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    coord_flip() + 
    geom_vline(x=18.5,y=partyguess_withsureness,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y=partyguess_withsureness,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y=partyguess_withsureness,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y=partyguess_withsureness,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y=partyguess_withsureness,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change in Guess Candidate is a Republican (Weighted by Sureness)")
dev.off()

###########################
###############
###Figure 6

minci <- (ideologyguess_withsureness - (1.96*ideologyguess_withsureness_se))
maxci <- (ideologyguess_withsureness + (1.96*ideologyguess_withsureness_se))
pdf("~/Desktop/ideologyguess_withsurenessDV.pdf", width=9, height=9, pointsize=10)
ggplot(results, 
    aes(x=iv_order, y=ideologyguess_withsureness, group=1)) + 
    geom_line(y=0,size=0.5,color="gray70") +
    scale_x_discrete(limits=rev(iv_order)) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci)) +
    geom_point(shape=21, size=2, fill="black") +
    ylim(-0.3,0.3) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    coord_flip() + 
    geom_vline(x=18.5,y= ideologyguess_withsureness,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y= ideologyguess_withsureness,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y= ideologyguess_withsureness,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y= ideologyguess_withsureness,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y= ideologyguess_withsureness,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change in Guess Candidate is Conservative (Weighted by Sureness)")
dev.off()

#######################
##Now for the knowledge trichotimization

results <- read.csv("allresults_knowtrichot.csv")

results$iv_label_list_forplot_alt <- c(
"Gender - Male",
"Gender - Female",
"Family - Unmarried",
"Family - Married",
"Family - Married with one son",
"Family - Married with one daughter",
"Family - Married with one daughter and one son",
"Family - Married with three sons",
"Family - Married with three daughters",
"Religion - None Listed",
"Religion - Catholic",
"Religion - Jewish",
"Religion - Mainline Protestant",
"Religion - Evangelical Protestant",
"Military - None",
"Military - National Guard",
"Military - US Army",
"Military - Major in US Army",
"Occupation - Retail Manager",
"Occupation - Small Business Owner",
"Occupation - Attorney",
"Occupation - Doctor",
"Occupation - CEO",
"Occupation - Farmer",
"Occupation - Teacher",
"Occupation - Factory Foreman",
"Occupation - Construction Contractor",
"Occupation - Political Staffer",
"Issue - Strengthening the economy",
"Issue - Strengthening national defense",
"Issue - Preventing future terrorist attacks",
"Issue - Promoting strong moral values",
"Issue - Addressing the immigration problem",
"Issue - Tax reform",
"Issue - Fighting against illegal drugs",
"Issue - Reducing crime",
"Issue - Promoting trade with other nations",
"Issue - Reducing the budget deficit",
"Issue - Creating new jobs",
"Issue - Promoting energy independence",
"Issue - Improving education",
"Issue - Strengthening Social Security",
"Issue - Preserving Medicare",
"Issue - Protecting the environment",
"Issue - Improving health care",
"Issue - Assistance to the poor and needy"
)

library(ggplot2)

results$iv_order <- factor(results$iv_label_list_forplot_alt, as.character(results$iv_label_list_forplot_alt))
attach(results)

###########################
###########################
###########################
###########################

stacked_by_knowledge <- data.frame(
iv_order=rev(c(as.character(iv_order),as.character(iv_order),as.character(iv_order))),
know=rev(c(rep("Low Knowledge Respondent",46),rep("Medium Knowledge Respondent",46),rep("High Knowledge Respondent",46))),
ideologyguess=rev(c(ideo_low,ideo_mid,ideo_high)),
ideologyguess_se=rev(c(ideo_low_se,ideo_mid_se,ideo_high_se)),
partyguess=rev(c(party_low,party_mid,party_high)),
partyguess_se=rev(c(party_low_se,party_mid_se,party_high_se))
)
stacked_by_knowledge$iv_order <- factor(stacked_by_knowledge$iv_order,as.character(stacked_by_knowledge$iv_order))

###########################
###########################
###########################
###########################
###Figure Ideology

attach(stacked_by_knowledge)

pd <- position_dodge(.5)
minci <- (stacked_by_knowledge$ideologyguess - (1.96* stacked_by_knowledge$ideologyguess_se))
maxci <- (stacked_by_knowledge$ideologyguess + (1.96* stacked_by_knowledge$ideologyguess_se))


pdf("~/Desktop/ideologyguess_byknowledge_trichot.pdf", width=9, height=9, pointsize=10)
ggplot(stacked_by_knowledge, 
    aes(x=iv_order, y=ideologyguess, colour=know)) + 
    geom_hline(y=0,size=0.5,color="gray70") +
    scale_color_manual(values = c("black","gray80","gray40")) +
    #scale_x_discrete(limits=rep(rev(iv_order),3) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.4,0.4) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(0.78,0.05),legend.title=element_blank()) +
    coord_flip() + 
    geom_vline(x=18.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y= ideologyguess,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y= ideologyguess,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Guess Candidate is Conservative)")
dev.off()

detach(stacked_by_knowledge)

###########################
###########################
###########################
###########################
###Figure Party

attach(stacked_by_knowledge)

pd <- position_dodge(.5)
minci <- (stacked_by_knowledge$partyguess - (1.96* stacked_by_knowledge$partyguess_se))
maxci <- (stacked_by_knowledge$partyguess + (1.96* stacked_by_knowledge$partyguess_se))


pdf("~/Desktop/partyguess_byknowledge_trichot.pdf", width=9, height=9, pointsize=10)
ggplot(stacked_by_knowledge, 
    aes(x=iv_order, y=partyguess, colour=know)) + 
    geom_hline(y=0,size=0.5,color="gray70") +
    scale_color_manual(values = c("black","gray80","gray40")) +
    #scale_x_discrete(limits=rep(rev(iv_order),3) + 
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.4,0.4) + 
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(0.78,0.05),legend.title=element_blank()) +
    coord_flip() + 
    geom_vline(x=18.5,y= partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=28.5,y= partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=32.5,y= partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=37.5,y= partyguess,size=0.5,linetype="dashed") +
    geom_vline(x=44.5,y= partyguess,size=0.5,linetype="dashed") +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Guess Candidate is Republican)")
dev.off()

detach(stacked_by_knowledge)


#######
#END
#######